package com.layui.ad.controller;

import com.layui.ad.entity.Level;
import com.layui.ad.query.LevelQuery;
import com.layui.ad.service.LevelService;
import com.layui.ad.utils.JsonResult;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/level")
public class LevelController {

    @Autowired
    private LevelService levelService;

    @GetMapping("/index")
    public String index(HttpServletRequest request) {
        return "level/index";
    }

    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(LevelQuery levelQuery) {
        return levelService.getList(levelQuery);
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Integer id) {
        Object level = new Level();
        if (id != null && id > 0) {
            level = levelService.getInfo(id);
        }
        request.setAttribute("info", level);
        return "level/edit.html";
    }

    @PostMapping("/add")
    @ResponseBody
    public JsonResult add(@RequestBody Level level) {
        return levelService.edit(level);
    }

    @PostMapping("/update")
    @ResponseBody
    public JsonResult update(@RequestBody Level level) {
        return levelService.edit(level);
    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    private JsonResult delete(@PathVariable("id") Integer id) {
        return levelService.delete(id);
    }

    @GetMapping("/batchDelete/{ids}")
    @ResponseBody
    public JsonResult batchDelete(@PathVariable("ids") String ids) {
        return levelService.batchDelete(ids);
    }

    @PostMapping("/batchStatus")
    @ResponseBody
    public JsonResult batchStatus(@RequestBody Level level) {
        return levelService.batchStatus(level);
    }

    @PostMapping("/setStatus")
    @ResponseBody
    public JsonResult setStatus(@RequestBody Level level) {
        return levelService.setStatus(level);
    }

}
